
package com.sijibao.gather.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringContainer implements Container
{

	private static final Logger logger = LoggerFactory
			.getLogger(SpringContainer.class);

	static ClassPathXmlApplicationContext context;
	
	private Thread handlerThread;

	public static ClassPathXmlApplicationContext getContext()
	{
		return context;
	}

	public void start()
	{

		context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
		context.start();
		ConsumerHandler consumerHandler=(ConsumerHandler)context.getBean("consumerHandler");
		handlerThread=new Thread(new Runnable(){

			@Override
			public void run()
			{
				consumerHandler.dispose();
				
			}});
		handlerThread.start();
	}

	public void stop()
	{
		try
		{
			if(handlerThread!=null)
			{
				handlerThread.interrupt();
			}
			
			if (context != null)
			{
				context.stop();
				context.close();
				context = null;
			}
		} catch (Throwable e)
		{
			logger.error(e.getMessage(), e);
		}
	}

}